package com.itheima.util;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

public class MySqlUtil {
    private static SqlSessionFactory factory;

    static {
        SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
        InputStream inputStream = null;
        try {
            inputStream = Resources.getResourceAsStream("mybatis-config.xml");
        } catch (IOException e) {
            e.printStackTrace();
        }
        factory = builder.build(inputStream);
    }

    // 获取工厂创造类对象
    public static SqlSessionFactory getSqlSessionFactory(){
        return factory;
    }

    // 获取会话对象
    public static SqlSession getSqlSession(){
        return factory.openSession();
    }

    public static void main(String[] args) {
        System.out.println("test");
//        getSqlSessionFactory();
//        getSqlSession();
    }
}
